package com.yami.shop.cp.api.controller;


import cn.hutool.core.util.IdUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.google.common.collect.Maps;
import com.yami.shop.bean.app.param.CheckRegisterSmsParam;
import com.yami.shop.bean.app.param.SendSmsParam;
import com.yami.shop.bean.enums.SendType;
import com.yami.shop.common.exception.YamiShopBindException;
import com.yami.shop.common.i18n.I18nMessage;
import com.yami.shop.common.util.RedisUtil;
import com.yami.shop.cp.comment.enums.UserSubmitPersonEnum;
import com.yami.shop.cp.comment.model.UserSubmitPerson;
import com.yami.shop.cp.comment.service.UserSubmitPersonService;
import com.yami.shop.security.api.util.SecurityUtils;
import com.yami.shop.service.SmsLogService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;
import java.util.Objects;

@RestController
@RequestMapping("/p/cp/submit/person")
@Api(tags = "提报人接口")
@AllArgsConstructor
public class UserSubmitPersonController {

    private final UserSubmitPersonService userSubmitPersonService;
    private final SmsLogService smsLogService;

    private final String CHECK_SUBMIT_PERSON_SMS_FLAG = "checkSubmitPersonSmsFlag";

    @PostMapping("/add")
    @ApiOperation(value = "添加提报人")
    public ResponseEntity<UserSubmitPerson> addSubmitPerson(@Valid @RequestBody UserSubmitPerson userSubmitPerson) {
        String userId = SecurityUtils.getUser().getUserId();
        UserSubmitPerson userSubmitPersonOld = userSubmitPersonService.getOne(new LambdaQueryWrapper<UserSubmitPerson>()
                .eq(UserSubmitPerson::getUserId,userId).eq(UserSubmitPerson::getMobile,userSubmitPerson.getMobile()));
        if (Objects.isNull(userSubmitPersonOld)){
            userSubmitPerson.setUserId(userId);
            userSubmitPersonService.save(userSubmitPerson);
            return ResponseEntity.ok(userSubmitPerson);
        }
        userSubmitPersonOld.setStatus(UserSubmitPersonEnum.NORMAL.getValue());
        userSubmitPersonOld.setName(userSubmitPerson.getName());
        userSubmitPersonOld.setDepartment(userSubmitPerson.getDepartment());
        userSubmitPersonService.updateById(userSubmitPersonOld);
        return ResponseEntity.ok(userSubmitPersonOld);
    }

    @PutMapping("/update")
    @ApiOperation(value = "修改提报人")
    public ResponseEntity<String> updateSubmitPerson(@Valid @RequestBody UserSubmitPerson userSubmitPerson) {
        UserSubmitPerson userSubmitPersonOld = userSubmitPersonService.getById(userSubmitPerson.getId());
        // 判断修改提报人的状态是否正常
        if (Objects.isNull(userSubmitPersonOld) && !Objects.equals(UserSubmitPersonEnum.NORMAL.getValue(), userSubmitPersonOld.getStatus())) {
            return ResponseEntity.badRequest().body(I18nMessage.getMessage("yami.submit.person.status.error"));
        }
        userSubmitPersonService.updateById(userSubmitPerson);
        return ResponseEntity.ok(I18nMessage.getMessage("yami.submit.person.modified.successfully"));
    }

    @DeleteMapping("/delete/{id}")
    @ApiOperation(value = "删除提报人", notes = "不进行物理删除，修改提报人状态")
    public ResponseEntity<String> deleteSubmitPerson(@PathVariable("id") String submitPersonId) {
        // 将提报人状态改为删除状态
        UserSubmitPerson userSubmitPerson = userSubmitPersonService.getById(submitPersonId);
        userSubmitPerson.setStatus(UserSubmitPersonEnum.DELETE.getValue());
        userSubmitPersonService.updateById(userSubmitPerson);
        return ResponseEntity.ok(I18nMessage.getMessage("yami.submit.person.delete.successfully"));
    }

    @GetMapping("/info")
    @ApiOperation(value = "查询当前用户的提报人")
    public ResponseEntity<List<UserSubmitPerson>> submitPersonInfoByUserId() {
        String userId = SecurityUtils.getUser().getUserId();
        List<UserSubmitPerson> list = userSubmitPersonService.list(new LambdaQueryWrapper<UserSubmitPerson>()
                .eq(UserSubmitPerson::getUserId, userId)
                .eq(UserSubmitPerson::getStatus, UserSubmitPersonEnum.NORMAL.getValue()));
        return ResponseEntity.ok(list);
    }

    @GetMapping("/info/{id}")
    @ApiOperation(value = "根据提报人id查询提报人")
    public ResponseEntity<UserSubmitPerson> submitPersonInfoById(@PathVariable("id") String submitPersonId) {
        String userId = SecurityUtils.getUser().getUserId();
        UserSubmitPerson userSubmitPerson = userSubmitPersonService.getOne(new LambdaQueryWrapper<UserSubmitPerson>()
                .eq(UserSubmitPerson::getId, submitPersonId)
                .eq(UserSubmitPerson::getUserId, userId));
        return ResponseEntity.ok(userSubmitPerson);
    }


}
